package com.lex.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.lex.dialoglibrary.CustomDialog;
import com.lex.entity.ContactEntity;
import com.lex.entity.RecordEntity;
import com.lex.samismyfather.R;
import com.lex.widget.StateImageButton;
import com.lex.widget.text_view.CustomFontTextView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HistoryRecordAdapter extends BaseAdapter{
    private Context context;
    private List<RecordEntity>recordEntityList;

    public HistoryRecordAdapter(Context context,List<RecordEntity>recordEntityList){
        this.context = context;
        this.recordEntityList = recordEntityList;
    }
    @Override
    public int getCount() {
        return recordEntityList.size();
    }

    @Override
    public Object getItem(int position) {
        return recordEntityList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null){
            convertView = View.inflate(context,R.layout.item_history_his,null);
            viewHolder = new ViewHolder();
            viewHolder.hisImg = (ImageView) convertView.findViewById(R.id.his_img);
            viewHolder.hisContent = (CustomFontTextView) convertView.findViewById(R.id.his_content);
            viewHolder.hisFrom = (CustomFontTextView) convertView.findViewById(R.id.his_from);
            viewHolder.hisTime = (CustomFontTextView) convertView.findViewById(R.id.his_time);
            viewHolder.hisBtn = (StateImageButton) convertView.findViewById(R.id.his_btn);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final RecordEntity entity = (RecordEntity) getItem(position);
        RecordEntity.RECORD_TYPE recordType = entity.getHisImg();
        int[] recordTypeDrawables = {R.mipmap.tmb_contact,R.mipmap.tmb_url,R.mipmap.tmb_text};
        viewHolder.hisImg.setImageResource(recordTypeDrawables[recordType.ordinal()]);


        if (recordType == RecordEntity.RECORD_TYPE.CONTACT){
            ContactEntity contactEntity = (ContactEntity) entity.getHisContent();
            viewHolder.hisContent.setText(contactEntity.getName());
        }else{
            viewHolder.hisContent.setText((String)entity.getHisContent());
        }


        int[] hisFromResIds = {R.string.make,R.string.scan};
        int[] hisFromColor = {context.getResources().getColor(R.color.get_way_make_txt_color),context.getResources().getColor(R.color.get_way_scan_txt_color)};
        viewHolder.hisFrom.setTextColor(hisFromColor[entity.getHisFrom().ordinal()]);
        viewHolder.hisFrom.setText(hisFromResIds[entity.getHisFrom().ordinal()]);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(context.getString(R.string.record_time_formater));
        String time = simpleDateFormat.format(new Date(entity.getHisTime()));
        viewHolder.hisTime.setText(time);

        viewHolder.hisBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final CustomDialog removeRecordDialog = new CustomDialog(context,R.layout.dialog_remove_record_dialog);
                ((CustomFontTextView)removeRecordDialog.findViewById(R.id.his_content)).setText(viewHolder.hisContent.getText());
                removeRecordDialog.findViewById(R.id.confirm_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        recordEntityList.remove(position);
                        notifyDataSetChanged();
                        removeRecordDialog.dismiss();
                    }
                });

                removeRecordDialog.findViewById(R.id.cancel_btn).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeRecordDialog.dismiss();
                    }
                });
                removeRecordDialog.show();
            }
        });

        return convertView;
    }
    class ViewHolder{
        ImageView hisImg;
        CustomFontTextView hisContent,hisFrom,hisTime;
        StateImageButton hisBtn;
    }
}
